﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

using ID = System.String;
using Value = System.String;

namespace ResxusEngine
{
    public class Merger
    {
        /// <summary>
        /// 병합 우선순위 
        /// </summary>
        public enum MergePriority
        {
            Work,
            Base
        };
        
        /// <summary>
        /// 파일 병합
        /// </summary>
        /// <param name="fileAPath">원본 파일 A</param>
        /// <param name="fileBPath">원본 파일 B</param>
        /// <param name="outputFilePath">결과파일</param>
        public void Merge(MergePriority priority, FileInfo baseFilePath, FileInfo workFilePath, FileInfo outputFilePath)
        {
            try
            {
                Dictionary<ID, Values> compareTable = Common.GenerateTable(baseFilePath, workFilePath);
                Dictionary<ID, Value> listC = new Dictionary<ID, Value>();

                IDictionaryEnumerator enumerator = compareTable.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string value = String.Empty;

                    Values values = (Values)enumerator.Value;
                    switch (priority)
                    {
                        case MergePriority.Work:
                            value = values.ValueB.Equals(string.Empty) ? values.ValueA : values.ValueB;
                            break;
                        case MergePriority.Base:
                            value = values.ValueA.Equals(string.Empty) ? values.ValueB : values.ValueA;
                            break;
                        default:
                            break;
                    }

                    listC.Add(enumerator.Key.ToString(), value);
                }

                Writer.WriteResx(outputFilePath.FullName, listC.GetEnumerator());

                Console.WriteLine("Success! Merged at : {0}", outputFilePath.FullName);
                Common.FinishBeep();
            }
            catch (System.Exception ex)
            {
                Common.ErrorHandler(ex);
            }
        }
    }
}
